<?php $token = session('token');?>

@extends('admin.main')

@section('content')
<div class="wrapper wrapper-content">
	<div class="ibox float-e-margins">
		<div class="ibox-title">
			<h5>练习题维护</h5>
		</div>
		<div class="ibox-content">
			<div class="m-b">
    		<a href="javascript:;" onclick="RenderPageList()" class="btn btn-default m-l-xs"><i class="fa fa-refresh"></i> 刷新</a>
				<a href="{{url('admin/exam/add-topic')}}" class="btn btn-warning m-l"><i class="fa fa-plus"></i> 新增练习题</a>
			</div>
			<table id="tblDataList" class="table table-striped table-hover">
				<thead>
					<tr>
						<th width="65">#</th>
						<th width="100">类型</th>
						<th>题目</th>
						<th width="65"></th>
						<th width="65"></th>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
			<div id="pagination" class="text-center"></div>
		</div>
	</div>
</div>

<script id="tplDataList" type="text/html">
	<%each rows as row i%>
	<tr data-id="<%row.id%>">
		<td><%i+1%></td>
		<td><label class="label"><%row.typeText%></label></td>
		<td><%row.topic%></td>
		<td>
			<a href="javascript:;" class="m-r-xs js-edit">
				<i class="fa fa-pencil-square fa-lg"></i>编辑
			</a>
		</td>
		<td>
			<a href="javascript:;" class="m-r-xs js-del">
				<i class="fa fa-times-circle fa-lg"></i>删除
			</a>
		</td>
	</tr>
	<%/each%>
</script>
@endsection

@section('pagescript')
<script>
	$("#side-menu li[rel='examtopic']").addClass("active")
	  .parents("ul").addClass("in").parents("li").addClass("active");

	var artDialog, filter = {"page":1,"limit":10};
	var RenderPageList = function(){
		$("#tblDataList tbody").html('<tr><td colspan="10"><p class="ibox-loading-31">&nbsp;</p></td></tr>');
		$.get("{{url('admin/exam/page-rows')}}", filter, function(data){
			for(var i in data.rows){
        if(data.rows[i].topic){
          data.rows[i].topic = $(data.rows[i].topic).text().substr(0, 160);
        }
      }
			$("#tblDataList tbody").html(template("tplDataList", data));
			$("#pagination").html(page(data.page_count, data.total_rows, data.page));
		}).fail(failure);
	};

	$("#pagination").delegate("li a", "click", function(){
		filter.page = $(this).attr("rel");
		RenderPageList();
	});

  $("#tblDataList").delegate(".js-edit", "click", function(){
  	var id = $(this).parents('tr').eq(0).data("id");
  	window.location.href = "{{url('admin/exam/edit')}}?id=" + id;
  	return false;
	});

  $("#tblDataList").delegate(".js-del", "click", function(e){
		var id = $(this).parents('tr').eq(0).data("id");
		dialog({
			content: '<i class="fa fa-info-circle"></i> 确定要删除此题目吗？',
			ok: function(){
		  	$.post("{{url('admin/exam/delete')}}", {"id":id}, function(){
		  		$(e.target).parents("tr").eq(0).remove();
		  	}).fail(failure);
		  },
			cancel: true
		}).showModal();
	});

	$(function(){
		RenderPageList();
	});
</script>
@endsection